package org.easy.excel.support.parse.source;

import cn.hutool.core.date.LocalDateTimeUtil;
import lombok.Data;
import org.easy.excel.support.annotations.ExportTopLabel;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

import static org.easy.excel.support.annotations.ExportTopLabel.DEFAULT_SHEET_NAME;

/**
 * {@link org.easy.excel.support.annotations.ExportTopLabel} 注解的数据源
 *
 * @author 李坤
 * @date 2022/4/25 19:57
 */
@Data
public class ExcelTopLabelSource implements Serializable {

    /**
     * 默认的实例对象
     */
    public static final ExcelTopLabelSource EMPTY_EXCEL_TOP_TITLE = new ExcelTopLabelSource();

    static {
        EMPTY_EXCEL_TOP_TITLE.setHighly(1);
        EMPTY_EXCEL_TOP_TITLE.setAppendSequence(false);
        EMPTY_EXCEL_TOP_TITLE.setSheetName(DEFAULT_SHEET_NAME);
        EMPTY_EXCEL_TOP_TITLE.setFileName(String.valueOf(System.currentTimeMillis()));
        EMPTY_EXCEL_TOP_TITLE.setTopTitle(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 标题所占高度
     */
    private int highly;

    /**
     * 顶级标题
     */
    private String topTitle;

    /**
     * 导出文件名称
     */
    private String fileName;

    /**
     * 导出文件的 sheet 名称
     */
    private String sheetName;

    /**
     * 是否添加序列号
     */
    private boolean appendSequence;

    /**
     * 解析 ExportTopLabel 注解并获取 ExcelTopLabelSource 对象实例
     *
     * @param exportTopLabel ExportTopLabel 注解
     *
     * @return ExcelTopLabelSource 对象实例
     */
    public static ExcelTopLabelSource getInstance(ExportTopLabel exportTopLabel) {
        ExcelTopLabelSource excelTopLabelSource = EMPTY_EXCEL_TOP_TITLE;

        if (Objects.nonNull(exportTopLabel)) {
            excelTopLabelSource = new ExcelTopLabelSource();

            excelTopLabelSource.setTopTitle(exportTopLabel.value());
            excelTopLabelSource.setFileName(exportTopLabel.fileName());
            excelTopLabelSource.setSheetName(exportTopLabel.sheetName());
            excelTopLabelSource.setHighly(Math.max(exportTopLabel.highly(), 1));
            excelTopLabelSource.setAppendSequence(exportTopLabel.appendSequence());
        }
        return excelTopLabelSource;
    }

}
